package com.lin.lazybone.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lin.lazybone.R;
import com.lin.lazybone.entity.Order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mr.lin on 2017/11/14.
 */

public class WorkListAdapter extends BaseAdapter implements View.OnClickListener
{
    private List<Order> mList;
    private Context mContext;
    private LayoutInflater layoutInflater;
    private WorkCallback mWorkCallback;

    //保存小图标的id
    private Map<String, Integer> mDelTypeMap;

    public WorkListAdapter(Context mContext, List<Order> mList, WorkCallback mWorkCallback)
    {
        this.mContext = mContext;
        this.mList = mList;
        this.mWorkCallback = mWorkCallback;
        layoutInflater = LayoutInflater.from(mContext);
        //初始化快递信息
        mDelTypeMap = new HashMap<>();
        mDelTypeMap.put("顺丰", R.drawable.sf_mini);
        mDelTypeMap.put("申通", R.drawable.sto_mini);
        mDelTypeMap.put("中通", R.drawable.zto_mini);
        mDelTypeMap.put("ems", R.drawable.ems_mini);
        mDelTypeMap.put("百世汇通", R.drawable.best_mini);
        mDelTypeMap.put("圆通", R.drawable.yt_mini);
        mDelTypeMap.put("韵达", R.drawable.yunda_mini);
    }

    /**
     * 自定义接口回调点击时间
     */
    public interface WorkCallback
    {
        public void buttonclick(View v);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            viewHolder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.worklist_item, null);
            viewHolder.worklist_cancelbt = (Button) convertView.findViewById(R.id.worklist_cancelbt);
            viewHolder.worklist_deltype = (TextView) convertView.findViewById(R.id.worklist_deltype);
            viewHolder.worklist_item_image = (ImageView) convertView.findViewById(R.id.worklist_item_image);
            viewHolder.worklist_item_title = (TextView) convertView.findViewById(R.id.worklist_item_title);
            viewHolder.worklist_item_price = (TextView) convertView.findViewById(R.id.worklist_item_price);
            viewHolder.worklist_item_content = (TextView) convertView.findViewById(R.id.worklist_item_content);
            viewHolder.worklist_item_time = (TextView) convertView.findViewById(R.id.worklist_item_time);
            viewHolder.worklist_chatbt = (Button) convertView.findViewById(R.id.worklist_chatbt);
            viewHolder.worklist_donebt = (Button) convertView.findViewById(R.id.worklist_donebt);
            convertView.setTag(viewHolder);
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //设置数据
        Order order = mList.get(position);
        //设置需求类型
        viewHolder.worklist_deltype.setText(order.getOrder_type() ? "取快递" : "寄快递");
        //设置图片
        viewHolder.worklist_item_image.setBackgroundResource(mDelTypeMap.get(order.getOrder_del_type()));
        //设置标题
        viewHolder.worklist_item_title.setText(order.getOrder_del_type());
        //设置价格
        viewHolder.worklist_item_price.setText("￥ " + order.getOrder_price() + " RMB");
        //设置大概内容
        viewHolder.worklist_item_content.setText(order.getOrder_from() + "---->" + order.getOrder_to());
        //设置时间
        viewHolder.worklist_item_time.setText(order.getCreatedAt());
        //设置按钮的监听事件
        viewHolder.worklist_chatbt.setOnClickListener(this);
        viewHolder.worklist_chatbt.setTag(position);
        viewHolder.worklist_cancelbt.setOnClickListener(this);
        viewHolder.worklist_cancelbt.setTag(position);
        viewHolder.worklist_donebt.setOnClickListener(this);
        viewHolder.worklist_donebt.setTag(position);
        return convertView;
    }

    class ViewHolder
    {
        TextView worklist_deltype;
        ImageView worklist_item_image;
        TextView worklist_item_title;
        TextView worklist_item_price;
        TextView worklist_item_content;
        TextView worklist_item_time;
        Button worklist_chatbt;
        Button worklist_cancelbt;
        Button worklist_donebt;
    }

    @Override
    public void onClick(View v)
    {
        mWorkCallback.buttonclick(v);
    }


}
